package net.disy.cadenza.repository.wrappers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.disy.cadenza.associations.schema.AssociationList;
import net.disy.cadenza.dbmapping.schema.DatabaseMapping;
import net.disy.cadenza.objecttypes.schema.CadenzaObjectType;
import net.disy.cadenza.schema.objecttypecatalog.ObjectTypeCatalog;

public class ObjectTypeCatalogWrapper {
	private ObjectTypeCatalog objectTypeCatalog;
	private List<ObjectTypeListWrapper> objectTypeLists;
	private List<AssociationList> associationLists;
	
	public ObjectTypeCatalogWrapper(ObjectTypeCatalog objectTypeCatalog){
		this.objectTypeCatalog = objectTypeCatalog;
		objectTypeLists = new ArrayList<ObjectTypeListWrapper>();
		associationLists = new ArrayList<AssociationList>();
	}
	
	public void addObjectTypeList(ObjectTypeListWrapper objectTypeList){
		this.objectTypeLists.add(objectTypeList);
	}
	
	public void addAssociationList(AssociationList associationList){
		this.associationLists.add(associationList);
	}

	public List<ObjectTypeListWrapper> getObjectTypeLists() {
		return objectTypeLists;
	}

	public List<AssociationList> getAssociationLists() {
		return associationLists;
	}
	
	public CadenzaObjectType getObjectType(String objectTypeName){
		for(ObjectTypeListWrapper objectTypeList : objectTypeLists){
			CadenzaObjectType cadenzaObjectType = objectTypeList.getObjectType(objectTypeName);
			if(cadenzaObjectType != null)
				return cadenzaObjectType;
		}
		return null;
	}
	
	public Set<CadenzaObjectType> getObjectTypesForTable(String tableName){
		Set<CadenzaObjectType> objectTypes = new HashSet<CadenzaObjectType>();
		for(ObjectTypeListWrapper objectTypeList : objectTypeLists){
			objectTypes.addAll(objectTypeList.getObjectTypeForTable(tableName));
		}
		return objectTypes;
	}
	
	public Set<String> getObjectTypeAttributeNamesForTableColumn(String tableColumn){
		Set<String> objectTypeAttributeNames = new HashSet<String>();
		for(ObjectTypeListWrapper objectTypeList : objectTypeLists){
			objectTypeAttributeNames.addAll(objectTypeList.getObjectTypeAttributeNamesForTableColumn(tableColumn));
		}
		return objectTypeAttributeNames;
	}
	
	public DatabaseMapping getDatabaseMapping(String objectTypeName){
		for(ObjectTypeListWrapper objectTypeList : objectTypeLists){
			DatabaseMapping databaseMapping = objectTypeList.getDatabaseMapping(objectTypeName);
			if(databaseMapping != null)
				return databaseMapping;
		}
		return null;
	}
	
//	public List<DatabaseMapping> getDatabaseMappings(){
//		List<DatabaseMapping> databaseMappings = new ArrayList<DatabaseMapping>();
//		for(ObjectTypeListWrapper objectTypeList : objectTypeLists){
//			databaseMappings.addAll(objectTypeList.getDatabaseMappings());
//		}
//		return databaseMappings;
//	}
	
//	public List<DatabaseAttribute> getKeyDatabaseAttributes(String table){
//		List<DatabaseAttribute> keyDatabaseAttributes = new ArrayList<DatabaseAttribute>();
//		for(ObjectTypeListWrapper objectTypeList : objectTypeLists){
//			keyDatabaseAttributes = objectTypeList.getKeyDatabaseAttributes(table);
//			if(keyDatabaseAttributes.size() != 0)
//				return
//		}
//	}
}
